var app = getApp(), fetchApi = require("../../../utils/httpPost.js")
import Toast from '../../../component/vant/toast/toast';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    couponData:[],
    nick_name:'',
    qrcode_id:0,
    use_type:-1,
    isLoading:true,
    qrtype:'',
    band_user_id:0,
    terminal_num:'',
    terminal_name:'',
    terminal_id:0,
    estateName:'',
    radioSize:'1',
    radioCheck:'1',
    commentContent:'',
    check_state:1,
    fileList1: [],
    isclean:0,
    dicts:[],
    submitState:false,
    is_show_check_point:0,
    check_points:'',
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log('check options', options)
    if (options.num){
      this.data.terminal_num = options.num;
    }
    this.onHttpInit();
  },
  onHttpInit: function () {
    var that = this
    fetchApi.httpPost('/terminal/detail', {
      terminal_num: that.data.terminal_num,
    }, function (res) {
      console.log("获取二维码数据", res)
      console.log('res',res)
      if (res.data.code == 0) {
        that.data.qrcode_id = res.data.data.terminal.qrcode_id
        that.data.terminal_id = res.data.data.terminal.id
        that.setData({ 
          dicts:res.data.data.terminal.dicts,
          terminal_num: res.data.data.terminal.terminalNum,
          terminal_name: res.data.data.terminal.terminalName,
          is_show_check_point: res.data.data.terminal.isShowCheckPoint,
          check_points: res.data.data.terminal.checkPoints,
          estateName:res.data.data.terminal.estateName,
        })
      }else{
        Toast.fail(res.data.msg)
      }
      that.setData({ netWork: true, isLoading: false, })
    }, function () {
      that.setData({ netWork: false, hasData: false, isLoading: false, })
    })
  },
  trim: function (t) {
    return t.replace(/(^\s*)|(\s*$)/g, "");
  },
  submitOp:function(){
    var that = this, commentContent = this.trim(that.data.commentContent);
    console.log('commentContent',commentContent)
    console.log('commentContent.length', that.data.commentContent.length)
    if (commentContent.length <= 0){
      Toast.fail('情况说明内容不能为空');
      return;
    }
    if(that.data.submitState){
      Toast.fail('正在提交...');
      return;
    }
    that.data.submitState = true;
    that.setData({submitState : true })

    fetchApi.httpPost('/terminal/addCheck', {
      terminal_id: that.data.terminal_id,
      check_state: that.data.check_state,
      check_result: commentContent,
      is_clean: that.data.isclean
    }, function (res) {
      console.log("巡查上报返回数据", res)
      if (res.data.code == 0) {
        that.data.submitState = false;
        that.setData({submitState : false })
        var record_id = parseInt(res.data.data)
        Toast.success(res.data.msg);
        if (record_id > 0 && that.data.fileList1.length > 0) {
          for (var index in that.data.fileList1) {
            wx.uploadFile({
              url: fetchApi.getPlatformUrl() + '/terminal/uploadImg',
              filePath: that.data.fileList1[index].url,
              name: 'file',
              formData: { record_id: record_id },
              success: function (res) {
                console.log('uploadFile success res', res)
              }, fail: function (res) {
                console.log('uploadFile fail res', res)
              }
            })
          }
        }
        setTimeout(function () {
          wx.navigateBack({
            delta: 1,
          })
        }, 1000)
      }else{
        that.data.submitState = false;
        that.setData({submitState : false })
        Toast.fail( res.data.msg);
        return;
      }
    },function(){})
  },
  bindInput: function (t) {
    console.log("情况说明输入", t)
    this.data.commentContent = t.detail || "";
  },
  deleteImg:function(res){
    console.log('删除图片', res)
    var index = res.currentTarget.dataset.index;
    this.data.fileList1 = [];
    this.setData({
      fileList1: this.data.fileList1
    });
  },
  radioClick:function(res){
    console.log('radioClick',res)
    var name = res.target.dataset.name
    this.data.check_state = parseInt(name)
    this.setData({
      radioCheck:name
    })
  },
  afterRead(event) {
    console.log('afterRead',event)
    const { file, name } = event.detail;
    console.log('afterRead name',name)
    console.log('afterRead file',file)
    const fileList = this.data[`fileList${name}`];
    
    this.setData({ [`fileList${name}`]: fileList.concat(file) });
    
    console.log('afterRead fileList',fileList)
    this.data.fileList1.concat(file)
    console.log('fileList',this.data.fileList1)
  },
  oversize() {
    wx.showToast({ title: '文件超出大小限制', icon: 'none' });
  },
  reload:function(){
    
  }
})